package com.iflytek.jcy.supervise.clue.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iflytek.jcy.supervise.clue.common.pojo.CommonResponse;
import com.iflytek.jcy.supervise.clue.component.httpinterface.ModelHttpInterface;
import com.iflytek.jcy.supervise.clue.component.httpinterface.result.Model;
import com.iflytek.jcy.supervise.clue.component.httpinterface.result.OutModelResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 模型 前端控制器
 * </p>
 *
 * @author dgyu
 * @since 2022-10-20 */
@RequestMapping("/model")
@Api(value = "模型接口", tags = { "模型接口" })
@RestController
public class ModelController {
	private static final Logger logger = LoggerFactory.getLogger(ModelController.class);

	@Resource
    private ModelHttpInterface modelHttpInterface;

	@GetMapping("/all-enable")
	@ApiOperation(value = "获取所有已启用模型", notes = "获取所有已启用模型")
	public CommonResponse allEnable() {
		OutModelResponse<List<Model>> out= this.modelHttpInterface.getModels();
		return CommonResponse.success(out.getData());
	}

	@GetMapping("/full/{id}")
	@ApiOperation(value = "获取模型详情", notes = "获取模型详情")
	public CommonResponse full(@PathVariable(required = true,name = "id")Integer id) {
		OutModelResponse<Object> out= this.modelHttpInterface.getModelFull(id);
		return CommonResponse.success(out.getData());
	}

	@GetMapping("/getDateSetByModelId/{modelId}")
	@ApiOperation(value = "根据模型id获取数据集", notes = "根据模型id获取数据集")
	public CommonResponse getDateSetByModelId(@PathVariable(required = true,name = "modelId")Integer modelId) {
		OutModelResponse<Object> out= this.modelHttpInterface.getModelFull(modelId);
		Object object=out.getData();
		JSONArray jSONArrayResult=new JSONArray();
		if(object!=null){
			JSONObject jSONObject=JSONObject.parseObject(JSON.toJSONString(object));
			JSONArray jSONArray=jSONObject.getJSONArray("datasetList");
			if(jSONArray!=null && jSONArray.size()>0){
				for(int i=0;i<jSONArray.size();i++){
					JSONObject datasetJSONObject=jSONArray.getJSONObject(i);
					JSONObject data=new JSONObject();
					data.put("datasetId",datasetJSONObject.getIntValue("id"));
					data.put("datasetName",datasetJSONObject.getString("name"));
					jSONArrayResult.add(data);
				}
			}
		}
		return CommonResponse.success(jSONArrayResult);
	}


}
